﻿
/*======================================================================================================\
|                                          空调零售管理增加、修改页面                                                 |
|-------------------------------------------------------------------------------------------------------|
|作者：郭建文                                                                                           |
|时间：2010-09-09 创建并注释                                                                            |
|                                                                                                       |                                  |
|                                                                                                       |
|-------------------------------------------------------------------------------------------------------|
|                                                                                                       |
|                 必须严格执行代码层次缩进规范！4空格缩进！严禁tab键缩进！                              |
|                                                                                                       |
|                                       Copyright (c) 2009 YuJunJie                                     |
\======================================================================================================*/
//=======================================================================================
//定义一个以简单模式布局的页面，简单模式的page对象，包含一个frame页面结构对象
//frame页面结构对象包含其子元素 ：查询按钮、表单、表格
//=======================================================================================
var page1 = new Sail.page({
    
	name     : "pageDetail",                      //定义页面对象的唯一标识
    haveNavi : false,                           //未包含导航条

    frames   : {
	
        name     : "editFrame",                     //该frame对象的唯一标识
        title  : "空调零售单数据维护",
        getUrlParameter : ["id"],   
        getURL : "../../purchase.do?method=edit",
        postURL : "../../purchase.do?method=save",
        bindDataFirst : false,
        
      //-------------------------------------------------------------------------------
        //以JSON形式描述 frame 的构成(包含哪些 item)
        //-------------------------------------------------------------------------------
        items  : [
            {
                type  : "form", 
                name  : "formDetail",
                title : "空调零售单据",
                
                buttons : ["保存","关闭"],
            
                items : [
                    {
				        type:"input",
				        title:"单据编号",
				        allowBlank : false,
				        readOnly:true,
				        dataIndex:"dataset.billVo.billCode"
				    },
				    {
				        type:"calendar",
				        title:"单据日期",
				        allowBlank : false,   
				        dataIndex:"dataset.billVo.billDate"
				    },{
				        type:"input",
				        title:"总金额",
				        readOnly:true,
				        dataIndex:"dataset.billVo.value1"
				    },{
				        type:"input",
				        title:"已付款",
				        allowBlank : false,  
				        dataIndex:"dataset.billVo.value2"
				    },{
				        type:"input",
				        title:"欠款",
				        readOnly:false,
				        dataIndex:"dataset.billVo.value3"
				    },{
				        type:"input",
				        title:"客户姓名",
				        dataIndex:"dataset.billVo.value4"
				    },{
				        type:"input",
				        title:"联系电话",
				        dataIndex:"dataset.billVo.value5"
				    },{
				        type:"input",
				        title:"联系地址",
				        dataIndex:"dataset.billVo.value6"
				    },
		            {
				        type:"textArea",
				        title:"备注",
				        dataIndex:"dataset.billVo.value7",
				        height:50	
				            
				    }
                ],
            
                layoutConfig : {
                    columns : 2
                },
                layout : [
	                    {
				            items:[
				                {'name':'单据编号'},
				                {'name':'单据日期'}
				            ]
				        },{
				            items:[
				                {'name':'总金额'},
				                {'name':'已付款'}
				            ]
				        },{
				            items:[
					                {'name':'欠款'}
					            ]
					    }
				        ,{
				            items:[
				                {'name':'客户姓名'},
				                {'name':'联系电话'}
				            ]
				        },{
				            items:[
					                {'name':'联系地址','colspan':2}
					            ]
					        },{
				            items:[
				                {name:'备注','colspan':2}
				            ]
				        }
                ]
            },
            {
                name      : "gridSub",
                type      : "editGrid",
                title     : "空调零售单明细",
              
                tbar      : ['新增','-','删除','-'],
                bbar      : false,
                allowBlank  : false,
                useDefautlFuncForadd : false,
                bindRelation   : {
                    root  :"dataset.billVo.billDetailVo",
                    id    :"id",
                    field : [
                                
                    ],
                    col   :[
            				{"header":"Id",   "width":200,   align:"left", "dataIndex":"masterId",hidden:true},
						    {"header":"空调名称规格",   "width":200,   align:"left", "dataIndex":"materielName"},
						    {"header":"现存量",   "width":60,   align:"left","dataIndex":"stock"},
						    {"header":"单位Id",   "width":200,   align:"left", "dataIndex":"unitId",hidden:true},
						    {"header":"单位",   "width":60,   align:"left","dataIndex":"unitName"},
						    {"header":"数量",   "width":80,   align:"left","dataIndex":"amount"},
						    {"header":"单价",   "width":80,   align:"left","dataIndex":"price"},
						    {"header":"金额",   "width":80,   align:"left","dataIndex":"money"}
                    ],
                    "editor":[
                        {},
                        {},
                        {},
                        {},
                        {},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {} //type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}}
                    ]
                }
            }
        ],
        
        //-------------------------------------------------------------------------------
        //【注】 添加 businessData 空结构
        //-------------------------------------------------------------------------------
        businessData : {
            "dataset":{
              "billVo":{
                "id":"",    
                "billDate":"",
                "operator":"",
                "transactionId":"",
                "transactionTypeId":TRANSACTION_TYPE_ADD,
                "billTypeId":BILL_TYPE_RETAIL,
                "billCode":"",
                "fillTime":"",
                "billTypeName":"",
                "originEntityName":"",
                "targetEntityName":"",
                "originEntityId":"",
                "targetEntityId":"", 
                "value1":"",
                "value2":"",
                "value3":"",
                "value4":"",
                "value5":"",
                "value6":"",
                "value7":"",         
                "otherDic1Id":"",
                "otherDic2Id":"",
                "otherDic3Id":"",
                "otherDic4Id":"",
                "otherDic5Id":"",
                "otherDic6Id":"",
                "otherDic7Id":"",
                "otherDic8Id":"",
                "otherDic9Id":"",
                "otherDic10Id":"",   
                "otherDic1Name":"",
                "otherDic2Name":"",
                "otherDic3Name":"",
                "otherDic4Name":"",
                "otherDic5Name":"",
                "otherDic6Name":"",
                "otherDic7Name":"",
                "otherDic8Name":"",
                "otherDic9Name":"",
                "otherDic10Name":"",
                "remark":"",
                "sysFlag":SYS_FLAG_USEING,
                "creator":"",
                "createTime":"",
                "lastModifier":"",
                "lastModifiedTime":"",
                "billDetailVo":
                  {
                    "id":"",
                    "transactionDetailTypeId":TRANSACTION_DETAIL_TYPE_OUT,
                    "unitId":COMBOX_UNIT_VAULE,
                    "unitName":COMBOX_UNIT_NAME,
                    "materielId":"",
                    "materielName":"",
                    "price":"",
                    "money":"",
                    "amount":"",    
                    "stateId":"",
                    "stateName":"",
                    "weight":"",
                    "originEntityId":"",
                    "targetEntityId":"",
                    "originEntityName":"",
                    "targetEntityName":"",
                    "otherDic1Id":"",
                    "otherDic2Id":"",
                    "otherDic3Id":"",
                    "otherDic4Id":"",
                    "otherDic5Id":"",
                    "otherDic6Id":"",
                    "otherDic7Id":"",
                    "otherDic8Id":"",
                    "otherDic9Id":"",
                    "otherDic10Id":"",
                    "otherDic1Name":"",
                    "otherDic2Name":"",
                    "otherDic3Name":"",
                    "otherDic4Name":"",
                    "otherDic5Name":"",
                    "otherDic6Name":"",
                    "otherDic7Name":"",
                    "otherDic8Name":"",
                    "otherDic9Name":"",
                    "otherDic10Name":"",
                    "remark":"",
                    "sysFlag":SYS_FLAG_USEING,
                    "creator":"",
                    "createTime":"",
                    "lastModifier":"",
                    "lastModifiedTime":""
                  }
                
              }
            }
          },

        //-------------------------------------------------------------------------------
        //【注】为当前 frame 的items 绑定事件
        //-------------------------------------------------------------------------------
        bindEvent2Items : function(){    
        	var obj_grid = $('gridSub'); 
            var obj_form  = $('formDetail');

            $('formDetail').getButton('保存').on('click',function(){
            	
                this.saveBusiness("close");
            },this);
            
            obj_grid.addListener2Editor("单价",'afteredit',function(column,editInfo){
            	computeRowMoney(editInfo);
            });
            
            obj_grid.addListener2Editor("数量",'afteredit',function(column,editInfo){
            	computeRowMoney(editInfo);
            });
            
            $('formDetail').getButton('关闭').on('click',function(){
                this.close();
            },$('formDetail').ownerFrame);
            
            
            obj_grid.addButtonHandler('新增',function(){
                var items = Sail.open("materielSelector.html","list",{},600,520);
                if(!items || items.length===0) return;
                var obj_grid = this;
                var ids = [];
                for(var i=0;i<items.length;i++){
                	var record = obj_grid.createRecord.call(obj_grid);
		              record.editing = true;
                  record.set("transactionDetailTypeId",TRANSACTION_DETAIL_TYPE_OUT);
                  record.set("sysFlag",SYS_FLAG_USEING);
		              record.set('materielName', items[i].data.name || '');
		              record.set('materielId', items[i].data.id);
		              record.set('unitName', items[i].data.other2Name || '');
		              record.set('unitId', items[i].data.other2Id);
		              record.editing = false;
		              obj_grid.addRecord.call(obj_grid,record);
                }
            },obj_grid);
        },
        
        beforeBindData:function()
        {
        	if(this.getRequest("isCopyAdd") == 'true')// && this.getRequest("isCopyAdd") == "true")
        	{     	
        		var tempBaseInfo = this.businessData.dataset.outwarehouseinfovo.baseInfo;
        		tempBaseInfo.transPid = "";
        		tempBaseInfo.billPid = "";
        		tempBaseInfo.billDate = "";
        		var detailList = this.businessData.dataset.outwarehouseinfovo.detailList.outwarehousedetailvo;
        		if (typeof(detailList.length) == 'undefined')
            	{
            		detailList.detailPid = "";
            		detailList.transPid = ""; 
            		detailList.amount = 0;
            		detailList.count = 0; 
            	}
        		else
        		{
	            	for (var i = 0; i < detailList.length; i++)
	            	{
	            		var tempDetail = detailList[i];
	            		tempDetail.detailPid = "";
	            		tempDetail.transPid = ""; 
	            		tempDetail.amount = 0;
	            		tempDetail.count = 0;
	            	}        
        		}
        	}
        	return true;
        }
    },

	afterRun : function(){
    	$('单据编号').disable();
    	//$('录入人').disable();
    	
    	//增加时执行
    	if((this.getRequest().data.f_transId==null) 
    			|| (this.frames[0].getRequest("isCopyAdd") == 'true'))
    	{
    		$('单据编号').setValue("由系统自动生成");
    	//	$('录入人').setValue(this.getOpername());
	    }
	}
});

page1.run();

function computeRowMoney(gridRow)
{
	var amount = gridRow.record.data.amount;
    var price =gridRow.record.data.price;
    var oldMoney = gridRow.record.data.money;
    if (oldMoney == null || oldMoney == undefined)
    {
    	oldMoney = 0;
    }
    
    var totalMoney = $('总金额').getValue();
    if (totalMoney == null || totalMoney == undefined)
    {
    	totalMoney = 0;
    }
    
    totalMoney = totalMoney - oldMoney;
    
    var money = 0;
   
    if (amount != 0)
    {
    	money = Math.round(100 * price * amount) / 100; //保留2位小数
    }
    
    
    totalMoney = totalMoney + money;
    $('总金额').setValue(totalMoney);
    
    $('gridSub').setFieldValueByDataIndex("money", gridRow.row, money);
}

/**
 * 设置单据编码
 * @return
 */
function setBillCode(currDate){
	var _url  = "../../../DataMap?funcname=getnewbillcode&f_codePars=billDate:"+changeDateString(currDate);
	var _callback = function(flag,getData)
	{
		var str_billCode = getData.dataset.string;
		$('单据编号').setValue(str_billCode);
	}
	Sail.postData(_url,{},_callback);//调用方法
}